package com.example.demo.controller;

import com.example.demo.entity.RelSendRecords;
import com.example.demo.entity.SendRecordsInfo;
import com.example.demo.service.SendRecordsService;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author
 * @Description
 */
@RestController
@RequestMapping("/sendRecords")
public class SendRecordsController {
    @Autowired
    private SendRecordsService sendRecordsService;

    @PostMapping("/add")
    private Result addSendRecords(@RequestBody SendRecordsInfo sendRecordsInfo) {
//        List<String> invoiceIdList = new ArrayList<>();
//        for (int i = 1; i < 6; i++) {
//            invoiceIdList.add("200" + i);
//        }
//        SendRecordsInfo sendRecordsInfo = new SendRecordsInfo();
//        sendRecordsInfo.setEmail("123@email.com");
//        sendRecordsInfo.setPurchaseName("test2");
//        sendRecordsInfo.setSuccess(true);
//        sendRecordsInfo.setRequestSource("手动交付");
//        sendRecordsInfo.setInvoiceIdList(invoiceIdList);
        Long l = sendRecordsService.addSendRecordsList(sendRecordsInfo);
        Result result = new Result();
        if (l > 0) {
            result.setMessage("成功添加" + l + "条数据");
            result.setObject(l);
        } else {
            result.setMessage("失败");
        }
        return result;
    }

    @PostMapping("/find")
    private Result findSendRecords(@RequestBody SendRecordsInfo sendRecordsInfo) {
        String startTime = sendRecordsInfo.getStartTime();
        String endTime = sendRecordsInfo.getEndTime();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
//        try {
//            Date date = format.parse(startTime);
//            System.err.println(format1.format(date));
//            System.err.println(date);
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }
        if (StringUtils.hasText(startTime)) {
            String start = startTime.replace("-", "").replace(" ", "").replace(":", "");
            System.err.println(start);
            startTime = startTime.substring(0, 10).replace("-", "");
            startTime = startTime + "000000";
            sendRecordsInfo.setStartTime(startTime);
        }
        if (StringUtils.hasText(endTime)) {
            String end = endTime.replace("-", "").replace(" ", "").replace(":", "");
            System.err.println(end);
            endTime = endTime.substring(0, 10).replace("-", "");
            endTime = endTime + "235959";
            sendRecordsInfo.setEndTime(endTime);
        }

        List<SendRecordsInfo> sendRecordList = sendRecordsService.findSendRecords(sendRecordsInfo);
        Result result = new Result();
        if (sendRecordList != null && !sendRecordList.isEmpty()) {
            result.setMessage("成功获取" + sendRecordList.size() + "条数据");
            result.setObject(sendRecordList);
        } else {
            result.setMessage("失败");
        }
        return result;
    }

    @PostMapping("/findDetail")
    private Result findSendRecordsDetail(Long sendId) {
        Result result = new Result();
        List<RelSendRecords> relSendRecordsList = sendRecordsService.findRelSendRecords(sendId);
        result.setMessage("成功获取" + relSendRecordsList.size() + "条数据");
        result.setObject(relSendRecordsList);
        return result;
    }

    @PostMapping("/resend")
    private Result resend(@RequestBody SendRecordsInfo sendRecordsInfo) {
        // AuthUserInfo
        Result result = new Result();
        String email = sendRecordsInfo.getEmail();
        List<String> invoiceIdList = sendRecordsService.findInvoiceIdListBySendId(sendRecordsInfo.getSendId());
        System.out.println(email);
        System.out.println(invoiceIdList);
        // 新纪录添加原id 原纪录添加新id
//        List<InvoiceInfo> invoiceInfoList = invoiceService.findInvoiceListByIds(invoiceIdList);
//        result = invoiceService.batchSendInvoiceNew(, , email, invoiceInfoList);
        // 再次交付，关联表新增 修改
//        Long l = sendRecordsService.resend(sendRecordsInfo, email);
//        sendRecordsService.
        System.out.println("交付接口");

        return result;
    }

}
